Page({

  /**
   * 页面的初始数据
   */
  data: {
    https: 'https://csb2c.fenxiangqu.cn/',
    comm:null,
    newIndex: 1,
    spsl: 0,
    commnum: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    wx.showLoading({
      title: '加载中',
    })
    let self = this;
    let a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a,
      id: options.id,
      jg: options.jg,
      activityid: options.activityid,
      cx: options.cx,
      kc: options.kc
    })
    const id = options.id;
    console.log(options)
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_zb_000010x",
      data: {
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: res => {
        self.setData({
          comm:res.data,
          spsl:res.data[0].spkc,
          zh: (res.data[0].zhongbz ? res.data[0].zhongbz : 1)
        })
        console.log(self.data)
        wx.hideLoading()
      },
      fail: res => {
        wx.hideLoading()
        wx.showToast({
          title: '网络故障！',
          icon: 'success',
          duration: 1000
        })
      }
    })
    
  },
  startOne(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      newIndex: index

    })
  },
  cutCom: function (e) {
    const self = this;
    var num = self.data.commnum;
    if (num > 1) {
      num--;
      self.setData({
        commnum: num
      })
    }
    else {
      wx.showToast({
        title: '数量不能小于等于0!',
        icon: 'success',
        duration: 1000
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  addCom(e) {
    const self = this;
    var num = self.data.commnum;
    num++;
    if (num >= 999) {
      self.setData({
        commnum: 999
      })
    } else if (num >= self.data.spsl) {
      if (self.data.spsl == 0) {
        wx.showToast({
          title: '抱歉,商品库存不足!',
          icon: 'success',
          duration: 1000
        })
      } else {
        wx.showToast({
          title: '抱歉,商品库存只有' + self.data.spsl + '件!',
          icon: 'success',
          duration: 1000
        })
      }
    } else {
      self.setData({
        commnum: num
      })
    }
  },
  /*加入购物车 */
  xlgwc(res){
    console.log('111')
    let self = this,
    e = self.data,
    id = e.id, cx = e.cx, activityID = e.activityid, jg = e.jg, kc = e.kc, num = e.commnum, xl = e.zh;
    console.log({
      xl: xl,
      id: id,
      activityID: activityID,
      type: cx
    })
    if(!num){
      wx.showToast({
        title: '数量有误',
        icon: 'success',
        duration: 2000
      })
      return false;
    }else if( (+num) % (+xl) !=0 ){
      wx.showToast({
        title: '数量有误',
        icon: 'success',
        duration: 2000
      })
    }else{
      xl = num;
    }
    if(+jg > 0.01 && jg != ""){
      
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_limited_buying_addShopCart_app",
        data: {
          xl: xl,
          id: id,
          activityID: activityID,
          type: cx
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: res => {
          if (res.data.state == "login"){
            wx.showModal({
              title: '登录提示',
              content: '您未登录，是否前往登录？',
              success: function (res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../center/center'
                  })
                } else if (res.cancel) {
                  wx.switchTab({
                    url: '../home/home'
                  })
                }
              }
            })
          }else{
            if (res.data.state == "2") {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 2000
              })
            } else if (res.data.state == "1") {
              wx.showToast({
                title: '库存不足',
                icon: 'success',
                duration: 2000
              })
            } else if (res.data.state == "3") {
              wx.showToast({
                title: '限制购买',
                icon: 'success',
                duration: 2000
              })

            } else if (res.data.state == "4") {
              wx.showToast({
                title: '活动过期',
                icon: 'success',
                duration: 2000
              })

            } else if (res.data.state == "5") {
              wx.showToast({
                title: '购物车存在商品',
                icon: 'success',
                duration: 2000
              })
            } else if (res.data.state == "0") {
              wx.showToast({
                title: '库存不足',
                icon: 'success',
                duration: 2000
              })
            } else {
                wx.showToast({
                  title: '添加失败',
                  icon: 'success',
                  duration: 2000
              })
            }
          }
        },
        fail: res => {
          wx.hideLoading()
          wx.showToast({
            title: '网络故障！',
            icon: 'success',
            duration: 1000
          })
        }
      })
    }
  },
  indexf() {
    wx.switchTab({
      url: '../home/home'
    })
  },
  goo() {
    wx.switchTab({
      url: '../goods/goods',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})